package com.gitee.grower

import com.gitee.grower.core.scheme.Operation
import com.gitee.grower.core.ui.GermLogicUI
import com.gitee.grower.core.ui.LogicUI
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.*
import taboolib.expansion.createHelper
import taboolib.platform.util.isAir

@CommandHeader("itemgrower", aliases = ["ig", "igrower"])
object ItemGrowerCommand {

    @CommandBody
    val main = mainCommand {
        createHelper()
    }

    @CommandBody
    val open = subCommand {
        dynamic("player") {
            suggestPlayers()

            execute<ProxyCommandSender> { sender, context, argument ->
                val player = Bukkit.getPlayerExact(argument) ?: return@execute
                val itemStack = player.inventory.itemInMainHand
                if (itemStack.isAir()) return@execute
                GermLogicUI(player,itemStack).open()
            }

        }
    }
    @CommandBody
    val modify = subCommand {
        dynamic("player") {
            suggestPlayers()

            execute<ProxyCommandSender> { sender, context, argument ->
                val player = Bukkit.getPlayerExact(argument) ?: return@execute
                commandModify(player,null)
            }

        }
    }

    @CommandBody
    val increase = subCommand {
        dynamic("player") {
            suggestPlayers()

            execute<ProxyCommandSender> { sender, context, argument ->
                val player = Bukkit.getPlayerExact(argument) ?: return@execute
                commandModify(player,Operation.INCREASE)
            }

        }
    }

    @CommandBody
    val decrease = subCommand {
        dynamic("player") {
            suggestPlayers()

            execute<ProxyCommandSender> { sender, context, argument ->
                val player = Bukkit.getPlayerExact(argument) ?: return@execute
                commandModify(player,Operation.DECREASE)
            }

        }
    }

    @CommandBody
    val reload = subCommand {
        execute<ProxyCommandSender> { sender, context, argument ->
            ItemGrower.config.reload()
            GermLogicUI.config.reload()
            ItemGrower.onEnable()
            sender.sendMessage("reload successful.")
        }
    }

    fun commandModify(player: Player,operation: Operation?) {
        val itemStack = player.inventory.itemInMainHand
        if (itemStack.isAir()) return

        when(operation) {

            Operation.INCREASE,Operation.DECREASE -> ItemGrowerAPI.modifyItemGrow(player, itemStack,operation)

            null -> ItemGrowerAPI.modifyItemGrow(player, itemStack)

        }


    }

}